package ex.mlist;



import java.util.List;

import Database.DBHelper;
import Database.Ringtone;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class ProfileActivity extends ListActivity {
    
	
	 AlertDialog.Builder alert;
	 ListView r_list;
	 List<Ringtone> lis;
	 DBHelper db;
	 int count;
	 int profile_id;
	 
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	profile_id=1;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profilelayout);
        
        db=new DBHelper(ProfileActivity.this);
        
        if(getIntent().hasExtra("profile_id")){
    		profile_id=getIntent().getExtras().getInt("profile_id",1);
    		}
       
        
        System.out.println(profile_id+"ghgh");
        
        
        
        
        r_list = getListView();
        
        r_list.setOnItemLongClickListener(new OnItemLongClickListener() {
			
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				final int position=arg2;
				alert=new AlertDialog.Builder(ProfileActivity.this);
				alert.setTitle("Delete");
		    	alert.setMessage("Confrim delete ");
		    	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				
					public void onClick(DialogInterface dialog, int which) {
						cancel(lis.get(position).getToneId());
						refreshList();
					}
				});
		    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	
					
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
		    	alert.create().show();
				return true;
			}
		});
        
        refreshList();
        
        
     
   }
    
    
    
    
    public void setButtonHandler(View v)
    {
    	Intent intent=new Intent(ProfileActivity.this, SetTime.class);
    	intent.putExtra("profile_id", profile_id);
		startActivity(intent);
    }
    
    
    
    protected void onListItemClick(ListView l, View v, int position, long id) {
		System.gc();
		//Toast.makeText(this,name,5).show();
		Intent intent=new Intent(ProfileActivity.this,EditTime.class);
		intent.putExtra("number",lis.get(position).getToneId() );
		intent.putExtra("strt_hrs",lis.get(position).getStartingHrs() );
		intent.putExtra("strt_min", lis.get(position).getStartingMinutes() );
		intent.putExtra("end_hrs",lis.get(position).getEndHrs() );
		intent.putExtra("end_min", lis.get(position).getEndMinutes() );
	
		intent.putExtra("ringtone", lis.get(position).getSource());
	
		startActivity(intent);
	}
	
    private void refreshList() {
    	
    	db.openDB();
    	lis=db.getRingtonesByProfilesId(profile_id);
    	db.close();
        r_list.setAdapter(new MusicAdapter(getApplicationContext(),lis));
        
	}


    public void cancel(int number)
	{
		Intent intent= new Intent(ProfileActivity.this, Schedule_service.class);
		PendingIntent sender = PendingIntent.getBroadcast(this, number, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
		am.cancel(sender);
		db.openDB();
		db.delete(number);
		db.close();
		
	}



	
    
    public boolean isIntentAvailable(Intent intent) {
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }
}